from django.core.exceptions import ValidationError
from django.db import models

from account.models import User
from relation.models import Client, Media
from utils.tools import gen_operation_id


class OperationList(models.Model):
    """ 运营表 """
    operation_id = models.ForeignKey(User, on_delete=models.SET_NULL, verbose_name='创建者', null=True,
                                     related_name='operation_id',
                                     help_text='<span style="color:red">默认就行，不用修改</span>')
    sn = models.CharField('编号ID', max_length=64, default=gen_operation_id)
    created_at = models.DateField('日期')
    operation_client = models.ForeignKey(Client, verbose_name='客户', on_delete=models.SET_NULL, related_name='client',
                                         null=True, blank=True)
    operation_media = models.ForeignKey(Media, verbose_name='媒体', on_delete=models.SET_NULL, related_name='media',
                                        null=True, blank=True)
    product = models.CharField('产品', max_length=100)
    sales = models.CharField('销售', max_length=32)
    account_name = models.CharField('账户名', max_length=200)
    consume = models.DecimalField('消耗', max_digits=32, decimal_places=2)
    consume_img = models.ImageField('消耗截图', upload_to='%Y%m/xiaohao', null=True, blank=True)
    client_rebate = models.DecimalField('给客户返点', max_digits=5, decimal_places=0)

    # 消耗现金 = 消耗 / (1 + 客户返点 /100)
    consume_cash = models.DecimalField('消耗现金', max_digits=32, decimal_places=2)
    conversions = models.IntegerField('转化数')

    # 成本(现金) = 消耗现金 / 转化数
    cost = models.DecimalField('成本(现金)', max_digits=32, decimal_places=2)
    copy = models.IntegerField('复制数', null=True, blank=True)
    check = models.CharField('考核成本', max_length=100, null=True, blank=True)
    operation = models.CharField('运营(对接)', max_length=100)
    media_rebate = models.DecimalField('媒体返点', max_digits=5, decimal_places=0)

    # 利润 = 消耗现金 - (消耗 / (1 + 02 /100))
    profit = models.DecimalField('利润', max_digits=32, decimal_places=2)
    updated_at = models.DateField('修改时间', auto_now=True)
    remark = models.TextField('备注', max_length=256, null=True, blank=True)
    is_valid = models.BooleanField('审核', default=False)

    class Meta:
        db_table = 'operation_list'
        verbose_name = '运营'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']

    def __str__(self):
        return '运营流水号：' + self.sn

    def clean(self):
        super().clean()
        if self.is_valid is True:
            raise ValidationError('已审核的无法修改')

        # 消耗现金 = 消耗 / (1 + 客户返点 /100)
        self.consume_cash = self.consume / (1 + self.client_rebate / 100)

        # 成本(现金) = 消耗现金 / 转化数
        self.cost = self.consume_cash / self.conversions

        # 利润 = 消耗现金 - (消耗 / (1 + 媒体返点 / 100))
        self.profit = self.consume_cash - (self.consume / (1 + self.media_rebate / 100))


class OperationReport(OperationList):
    """ 运营报表 """

    class Meta:
        verbose_name = '报表与审核'
        verbose_name_plural = verbose_name
        proxy = True
